#include "StdAfx.h"
#include "IrrBSPGeomLoader.h"
#include "IrrCamera.h"

#include "../Kernel.h"

extern irr::IrrlichtDevice*  g_IrrDevice;
extern Kernel* g_kernel;

IrrBSPGeomLoader::IrrBSPGeomLoader(void)
{
}

IrrBSPGeomLoader::~IrrBSPGeomLoader(void)
{
}


/** Load a Map */
IGeom* IrrBSPGeomLoader::LoadGeom( const std::string &GeomFile )
{
    if ( g_IrrDevice )
    {
        irr::scene::ISceneManager* sceneMng = g_IrrDevice->getSceneManager();
        IrrBSPGeom* Geom = new IrrBSPGeom( sceneMng );
        Geom->SetMesh ( sceneMng->getMesh( GeomFile.c_str() ) );

        if ( Geom->IsValid() ) 
        {
            Geom->SetSceneNode( sceneMng->addOctTreeSceneNode( Geom->GetMesh()->getMesh( 0 ) ) );
            return Geom;
        }
        else
            g_kernel->LPrintf( ("ERROR:: Could not find Irrlicht BSP file: " + GeomFile).c_str() );
    } 

    g_kernel->LPrintf( "ERROR:: Could not fine Irrlicht Device in IrrBSPMapLoader!" );
    return NULL;
}


/**------------------------------------------------------------------
    IrrBSPMap
---------------------------------------------------------------------*/

/** Initialize the map */
void IrrBSPGeom::Init()
{
    m_Camera = new IrrCamera;
    m_Camera->Init();

    if ( IsValid() && m_IrrNode )
    {
        irr::scene::ITriangleSelector* selector = m_IrrSceneMng->createOctTreeTriangleSelector( m_IrrMesh->getMesh( 0 ),
                                                                                                m_IrrNode, 128 );
        m_IrrNode->setTriangleSelector( selector );
        selector->drop();
   

        irr::scene::ICameraSceneNode* camera = m_Camera->GetCamera();
        irr::scene::ISceneNodeAnimator* anim = m_IrrSceneMng->createCollisionResponseAnimator( selector, camera, MVector3f( 30, 50, 30 ), MVector3f( 0, -100, 0) );
        camera->addAnimator( anim );
        anim->drop();

        /**
            TEMP CODE
        */
  //      irr::scene::IAnimatedMeshSceneNode* anms = m_IrrSceneMng->addAnimatedMeshSceneNode( m_IrrSceneMng->getMesh("./sydney.md2"));
  ////      anim = m_IrrSceneMng->createFlyStraightAnimator(core::vector3df(100,0,60), 
		////	                                    core::vector3df(-100,0,60), 10000, true);
		////anms->addAnimator(anim);
		////anim->drop();
  //      anms->setMaterialFlag(irr::video::EMF_LIGHTING, false);
  //      //anms->setFrameLoop(320, 360);
  //      anms->setMD2Animation( irr::scene::EMAT_SALUTE );

  //      anms->setAnimationSpeed(30);
  //      anms->setRotation(irr::core::vector3df(0,180.0f,0));
  //      anms->setMaterialTexture(0, g_IrrDevice->getVideoDriver()->getTexture("models/players/sarge/blue.tga"));
    }
}

/** Render the map */
void IrrBSPGeom::Render( IRender* r, ICamera* cam )
{
    m_IrrSceneMng->drawAll();
}



/** Free the Map */
void IrrBSPGeom::FreeGeom()
{
    // TODO
    if ( m_Camera )
        delete m_Camera;
}